using Entities;
using Microsoft.VisualBasic.ApplicationServices;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Xml.Linq;
using Utilities;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 工具
{
    public partial class Form1 : Form
    {
        List<Mpa> mpas = new List<Mpa>();
        List<SurfaceStatus> surfaces = new List<SurfaceStatus>();
        List<TwistingType> twistingTypes = new List<TwistingType>();
        List<Supplier> suppliers = new List<Supplier>();
        List<Producer> producers = new List<Producer>();
        List<Department> departments = new List<Department>();
        List<HoisterUsage> hoisterUsages = new List<HoisterUsage>();
        List<WireropeType> wireropeTypes = new List<WireropeType>();
        List<Hoister> hoisters = new List<Hoister>();
        List<InstallAddress> installAddresses = new List<InstallAddress>();
        List<StandStatus> standStatus = new List<StandStatus>();
        List<StandStatusDetail> standStatusDetails = new List<StandStatusDetail>();

        int jituanID = 1;  //TODO:固定值
        int userID = 1;
        string userName = "管理员";



        public Form1()
        {
            InitializeComponent();
            InitBaseData();
        }

        private void InitBaseData()
        {
            mpas = DataContext.Db.Queryable<Mpa>().ToList();
            surfaces = DataContext.Db.Queryable<SurfaceStatus>().ToList();
            twistingTypes = DataContext.Db.Queryable<TwistingType>().ToList();
            suppliers = DataContext.Db.Queryable<Supplier>().ToList();
            producers = DataContext.Db.Queryable<Producer>().ToList();
            departments = DataContext.Db.Queryable<Department>().ToList();
            hoisterUsages = DataContext.Db.Queryable<HoisterUsage>().ToList();
            wireropeTypes = DataContext.Db.Queryable<WireropeType>().ToList();
            hoisters = DataContext.Db.Queryable<Hoister>().ToList();
            installAddresses = DataContext.Db.Queryable<InstallAddress>().ToList();
            standStatus = DataContext.Db.Queryable<StandStatus>().ToList();
            standStatusDetails = DataContext.Db.Queryable<StandStatusDetail>().ToList();



        }

        private void button1_Click(object sender, EventArgs e)
        {
            //List<Stock> stocks = new List<Stock>();
            List<UsingWirerope> usingWireropes = new List<UsingWirerope>();
            List<CheckWirerope> checkWireropes = new List<CheckWirerope>();
            List<CheckWireropeDetail> checkWireropeDetails = new List<CheckWireropeDetail>();



            //读取excel中所有数据
            using (FileStream fs = new FileStream(@"F:\百克特导入数据.xlsx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var workbook = new XSSFWorkbook(fs);

                //获得库存excel数据
                #region 库存
                //var sheetKuCun = workbook.GetSheetAt(0);
                //for (int i = 1; i <= sheetKuCun.LastRowNum; i++)
                //{
                //    var row = sheetKuCun.GetRow(i);

                //    var temp = new Stock { };
                //    temp.GroupInfoID = jituanID;
                //    temp.Code = row.GetCell(1).ToString().Trim();
                //    temp.GuiGe = row.GetCell(2).ToString().Trim();
                //    temp.Diameter = row.GetCell(3).ToString().Trim() == "" ? default(decimal) : row.GetCell(3).ToString().Trim().ToDecimal();
                //    temp.Cnt = row.GetCell(4).ToString().Trim() == "" ? default(int) : row.GetCell(4).ToString().Trim().ToInt();
                //    temp.Name = row.GetCell(5).ToString().Trim();
                //    temp.NianJu = row.GetCell(6).ToString().Trim() == "" ? default(decimal) : row.GetCell(6).ToString().Trim().ToDecimal();
                //    //temp.MpaID = row.GetCell(7).ToString().Trim() == "" ? default(int) : row.GetCell(7).ToString().Trim().ToInt();
                //    //temp.SurfaceStatusID = row.GetCell(8).ToString().Trim() == "" ? default(int) : row.GetCell(8).ToString().Trim().ToInt();
                //    //temp.TwistingTypeID = row.GetCell(9).ToString().Trim() == "" ? default(int) : row.GetCell(9).ToString().Trim().ToInt();
                //    temp.LengthNum = row.GetCell(10).ToString().Trim() == "" ? default(decimal) : row.GetCell(10).ToString().Trim().ToDecimal();
                //    temp.KeepDay = row.GetCell(11).ToString().Trim() == "" ? default(int) : row.GetCell(11).ToString().Trim().ToInt();
                //    //temp.SupplierID = row.GetCell(12).ToString().Trim() == "" ? default(int) : row.GetCell(12).ToString().Trim().ToInt();
                //    //temp.ProducerID = row.GetCell(13).ToString().Trim() == "" ? default(int) : row.GetCell(13).ToString().Trim().ToInt();
                //    temp.DepartmentID = departments.Single(x => x.Name == row.GetCell(14).ToString().Trim()).DepartmentID;
                //    //temp.HoisterUsageID = row.GetCell(15).ToString().Trim() == "" ? default(int) : row.GetCell(15).ToString().Trim().ToInt();
                //    //temp.WireropeTypeID = row.GetCell(16).ToString().Trim() == "" ? default(int) : row.GetCell(16).ToString().Trim().ToInt();
                //    temp.IsOut = true;
                //    temp.Remark = row.GetCell(17).ToString().Trim();
                //    temp.NextKeepDate = row.GetCell(18).ToString().Trim() == "" ? DateTime.Now : row.GetCell(18).ToString().Trim().ToDateTime();

                //    //处理基础数据没有数据，先增加然后再使用
                //    if (!row.GetCell(7).ToString().Trim().IsEmpty())
                //    {
                //        var mpasTemp = mpas.FirstOrDefault(x => x.Name == row.GetCell(7).ToString().Trim());

                //        if (mpasTemp == null)
                //        {
                //            Mpa mpa = new Mpa();
                //            mpa.GroupInfoID = jituanID;
                //            mpa.Name = row.GetCell(7).ToString().Trim();
                //            mpa.IsDelete = false;
                //            mpa.CreateUserID = userID;
                //            mpa.UpdateUserID = userID;
                //            mpa.CreateUserName = userName;
                //            mpa.UpdateUserName = userName;
                //            mpa.CreateDate = DateTime.Now;
                //            mpa.UpdateDate = DateTime.Now;

                //            mpa.MpaID = DataContext.Db.Insertable(mpa).ExecuteReturnIdentity();
                //            mpas.Add(mpa);
                //            temp.MpaID = mpa.MpaID;
                //        }
                //        else
                //        {
                //            temp.MpaID = mpasTemp.MpaID;
                //        }
                //    }

                //    if (!row.GetCell(8).ToString().Trim().IsEmpty())
                //    {
                //        var surfaceTemp = surfaces.FirstOrDefault(x => x.Name == row.GetCell(8).ToString().Trim());

                //        if (surfaceTemp == null)
                //        {
                //            SurfaceStatus surface = new SurfaceStatus();
                //            surface.GroupInfoID = jituanID;
                //            surface.Name = row.GetCell(8).ToString().Trim();
                //            surface.IsDelete = false;
                //            surface.CreateUserID = userID;
                //            surface.UpdateUserID = userID;
                //            surface.CreateUserName = userName;
                //            surface.UpdateUserName = userName;
                //            surface.CreateDate = DateTime.Now;
                //            surface.UpdateDate = DateTime.Now;

                //            surface.SurfaceStatusID = DataContext.Db.Insertable(surface).ExecuteReturnIdentity();
                //            surfaces.Add(surface);
                //            temp.SurfaceStatusID = surface.SurfaceStatusID;
                //        }
                //        else
                //        {
                //            temp.SurfaceStatusID = surfaceTemp.SurfaceStatusID;
                //        }
                //    }

                //    if (!row.GetCell(9).ToString().Trim().IsEmpty())
                //    {
                //        var twistingTypesTemp = twistingTypes.FirstOrDefault(x => x.Name == row.GetCell(9).ToString().Trim());

                //        if (twistingTypesTemp == null)
                //        {
                //            TwistingType twistingType = new TwistingType();
                //            twistingType.GroupInfoID = jituanID;
                //            twistingType.Name = row.GetCell(9).ToString().Trim();
                //            twistingType.IsDelete = false;
                //            twistingType.CreateUserID = userID;
                //            twistingType.UpdateUserID = userID;
                //            twistingType.CreateUserName = userName;
                //            twistingType.UpdateUserName = userName;
                //            twistingType.CreateDate = DateTime.Now;
                //            twistingType.UpdateDate = DateTime.Now;

                //            twistingType.TwistingTypeID = DataContext.Db.Insertable(twistingType).ExecuteReturnIdentity();
                //            twistingTypes.Add(twistingType);
                //            temp.TwistingTypeID = twistingType.TwistingTypeID;
                //        }
                //        else
                //        {
                //            temp.TwistingTypeID = twistingTypesTemp.TwistingTypeID;
                //        }
                //    }

                //    if (!row.GetCell(12).ToString().Trim().IsEmpty())
                //    {
                //        var suppliersTemp = suppliers.FirstOrDefault(x => x.Name == row.GetCell(12).ToString().Trim());

                //        if (suppliersTemp == null)
                //        {
                //            Supplier supplier = new Supplier();
                //            supplier.GroupInfoID = jituanID;
                //            supplier.Name = row.GetCell(12).ToString().Trim();
                //            supplier.IsDelete = false;
                //            supplier.CreateUserID = userID;
                //            supplier.UpdateUserID = userID;
                //            supplier.CreateUserName = userName;
                //            supplier.UpdateUserName = userName;
                //            supplier.CreateDate = DateTime.Now;
                //            supplier.UpdateDate = DateTime.Now;

                //            supplier.SupplierID = DataContext.Db.Insertable(supplier).ExecuteReturnIdentity();
                //            suppliers.Add(supplier);
                //            temp.SupplierID = supplier.SupplierID;
                //        }
                //        else
                //        {
                //            temp.SupplierID = suppliersTemp.SupplierID;
                //        }
                //    }

                //    if (!row.GetCell(13).ToString().Trim().IsEmpty())
                //    {
                //        var producersTemp = producers.FirstOrDefault(x => x.Name == row.GetCell(13).ToString().Trim());

                //        if (producersTemp == null)
                //        {
                //            Producer producer = new Producer();
                //            producer.GroupInfoID = jituanID;
                //            producer.Name = row.GetCell(13).ToString().Trim();
                //            producer.IsDelete = false;
                //            producer.CreateUserID = userID;
                //            producer.UpdateUserID = userID;
                //            producer.CreateUserName = userName;
                //            producer.UpdateUserName = userName;
                //            producer.CreateDate = DateTime.Now;
                //            producer.UpdateDate = DateTime.Now;

                //            producer.ProducerID = DataContext.Db.Insertable(producer).ExecuteReturnIdentity();
                //            producers.Add(producer);
                //            temp.ProducerID = producer.ProducerID;
                //        }
                //        else
                //        {
                //            temp.ProducerID = producersTemp.ProducerID;
                //        }
                //    }

                //    if (!row.GetCell(15).ToString().Trim().IsEmpty())
                //    {
                //        var hoisterUsagesTemp = hoisterUsages.FirstOrDefault(x => x.Name == row.GetCell(15).ToString().Trim());

                //        if (hoisterUsagesTemp == null)
                //        {
                //            HoisterUsage hoisterUsage = new HoisterUsage();
                //            hoisterUsage.GroupInfoID = jituanID;
                //            hoisterUsage.Name = row.GetCell(15).ToString().Trim();
                //            hoisterUsage.IsDelete = false;
                //            hoisterUsage.CreateUserID = userID;
                //            hoisterUsage.UpdateUserID = userID;
                //            hoisterUsage.CreateUserName = userName;
                //            hoisterUsage.UpdateUserName = userName;
                //            hoisterUsage.CreateDate = DateTime.Now;
                //            hoisterUsage.UpdateDate = DateTime.Now;

                //            hoisterUsage.HoisterUsageID = DataContext.Db.Insertable(hoisterUsage).ExecuteReturnIdentity();
                //            hoisterUsages.Add(hoisterUsage);
                //            temp.HoisterUsageID = hoisterUsage.HoisterUsageID;
                //        }
                //        else
                //        {
                //            temp.HoisterUsageID = hoisterUsagesTemp.HoisterUsageID;
                //        }
                //    }

                //    if (!row.GetCell(16).ToString().Trim().IsEmpty())
                //    {
                //        var wireropeTypesTemp = wireropeTypes.FirstOrDefault(x => x.Name == row.GetCell(16).ToString().Trim());

                //        if (wireropeTypesTemp == null)
                //        {
                //            WireropeType wireropeType = new WireropeType();
                //            wireropeType.GroupInfoID = jituanID;
                //            wireropeType.Name = row.GetCell(16).ToString().Trim();
                //            wireropeType.IsDelete = false;
                //            wireropeType.CreateUserID = userID;
                //            wireropeType.UpdateUserID = userID;
                //            wireropeType.CreateUserName = userName;
                //            wireropeType.UpdateUserName = userName;
                //            wireropeType.CreateDate = DateTime.Now;
                //            wireropeType.UpdateDate = DateTime.Now;

                //            wireropeType.WireropeTypeID = DataContext.Db.Insertable(wireropeType).ExecuteReturnIdentity();
                //            wireropeTypes.Add(wireropeType);
                //            temp.WireropeTypeID = wireropeType.WireropeTypeID;
                //        }
                //        else
                //        {
                //            temp.WireropeTypeID = wireropeTypesTemp.WireropeTypeID;
                //        }
                //    }


                //    //建立BillDetail和Bill数据
                //    Bill bill = new Bill();
                //    bill.GroupInfoID = jituanID;
                //    bill.BillDate = DateTime.Now;
                //    bill.InOutType = 1;
                //    bill.OperTypeID = 1;   //TODO: 固定值
                //    bill.WarehouseID = 1;   //TODO: 固定值
                //    bill.ApplyUserID = userID;
                //    bill.ApplyUserName = userName;
                //    bill.SubmitUserID = userID;
                //    bill.SubmitUserName = userName;
                //    bill.SubmitDate = DateTime.Now;
                //    bill.ConfirmUserID = userID;
                //    bill.ConfirmUserName = userName;
                //    bill.ConfirmDate = DateTime.Now;
                //    bill.Status = 110;
                //    bill.CreateUserID = userID;
                //    bill.CreateUserName = userName;
                //    bill.CreateDate = DateTime.Now;

                //    bill.BillID = DataContext.Db.Insertable(bill).ExecuteReturnIdentity();


                //    BillDetail billDetail = new BillDetail();
                //    billDetail.GroupInfoID = jituanID;
                //    billDetail.BillID = bill.BillID;
                //    billDetail.Code = temp.Code;
                //    billDetail.WarehouseID = 1;   //TODO: 固定值
                //    billDetail.WarehousePositionID = 1;   //TODO: 固定值
                //    billDetail.GuiGe = temp.Code;
                //    billDetail.Diameter = temp.Diameter;
                //    billDetail.Cnt = temp.Cnt;
                //    billDetail.Name = temp.Name;
                //    billDetail.NianJu = temp.NianJu;
                //    billDetail.MpaID = temp.MpaID;
                //    billDetail.SurfaceStatusID = temp.SurfaceStatusID;
                //    billDetail.TwistingTypeID = temp.TwistingTypeID;
                //    billDetail.LengthNum = temp.LengthNum;
                //    billDetail.KeepDay = temp.KeepDay;
                //    billDetail.SupplierID = temp.SupplierID;
                //    billDetail.ProducerID = temp.ProducerID;
                //    billDetail.DepartmentID = temp.DepartmentID;
                //    billDetail.HoisterUsageID = temp.HoisterUsageID;
                //    billDetail.WireropeTypeID = temp.WireropeTypeID;
                //    billDetail.Remark = temp.Remark;

                //    billDetail.BillDetailID = DataContext.Db.Insertable(billDetail).ExecuteReturnIdentity();


                //    temp.BillID = bill.BillID;
                //    temp.BillDetailID = billDetail.BillDetailID;


                //    temp.StockID = DataContext.Db.Insertable(temp).ExecuteReturnIdentity();

                //    stocks.Add(temp);

                //} 
                #endregion

                //获得在用绳excel数据
                var sheetUsingWirerope = workbook.GetSheetAt(0);
                for (int i = 1; i <= sheetUsingWirerope.LastRowNum; i++)
                {
                    var row = sheetUsingWirerope.GetRow(i);

                    UsingWirerope usingWirerope = new UsingWirerope();

                    //通过绳子编码获得绳子数据
                    usingWirerope.GroupInfoID = jituanID;
                    usingWirerope.Code = row.GetCell(1).ToString().Trim();
                    usingWirerope.GuiGe = row.GetCell(11).ToString().Trim();
                    usingWirerope.Diameter = row.GetCell(12).ToString().Trim() == "" ? default(decimal) : row.GetCell(12).ToString().Trim().ToDecimal();
                    usingWirerope.Cnt = row.GetCell(13).ToString().Trim() == "" ? default(int) : row.GetCell(13).ToString().Trim().ToInt();
                    usingWirerope.Name = row.GetCell(14).ToString().Trim();
                    usingWirerope.NianJu = row.GetCell(15).ToString().Trim() == "" ? default(decimal) : row.GetCell(15).ToString().Trim().ToDecimal();

                    usingWirerope.LengthNum = row.GetCell(19).ToString().Trim() == "" ? default(decimal) : row.GetCell(19).ToString().Trim().ToDecimal();

                    usingWirerope.DepartmentID = departments.Single(x => x.Name == row.GetCell(22).ToString().Trim()).DepartmentID;

                    //usingWirerope.HoisterID = row.GetCell(2).ToString().Trim() == "" ? default(int) : row.GetCell(2).ToString().Trim().ToInt();
                    usingWirerope.InstallDate = row.GetCell(3).ToString().Trim() == "" ? DateTime.Now : row.GetCell(3).ToString().Trim().ToDateTime();
                    usingWirerope.InstallLengthNum = row.GetCell(4).ToString().Trim() == "" ? default(decimal) : row.GetCell(4).ToString().Trim().ToDecimal();
                    //usingWirerope.InstallAddressID = row.GetCell(5).ToString().Trim() == "" ? default(int) : row.GetCell(5).ToString().Trim().ToInt();
                    usingWirerope.LinkUser = row.GetCell(6).ToString().Trim();
                    usingWirerope.LinkPhone = row.GetCell(7).ToString().Trim();
                    usingWirerope.Remark = row.GetCell(8).ToString().Trim();
                    usingWirerope.OutStockDate = row.GetCell(9).ToString().Trim() == "" ? DateTime.Now : row.GetCell(9).ToString().Trim().ToDateTime();
                    usingWirerope.NextCheckDate = row.GetCell(10).ToString().Trim() == "" ? DateTime.Now : row.GetCell(10).ToString().Trim().ToDateTime();
                    usingWirerope.Status = 1;
                    usingWirerope.CreateUserID = userID;
                    usingWirerope.CreateUserName = userName;
                    usingWirerope.CreateDate = DateTime.Now;
                    usingWirerope.UpdateUserID = userID;
                    usingWirerope.UpdateUserName = userName;
                    usingWirerope.UpdateDate = DateTime.Now;

                    //处理基础数据没有数据，先增加然后再使用
                    if (!row.GetCell(2).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.HoisterName = row.GetCell(2).ToString().Trim();
                        var hoistersTemp = hoisters.FirstOrDefault(x => x.Name == row.GetCell(2).ToString().Trim());

                        if (hoistersTemp == null)
                        {
                            Hoister hoister = new Hoister();
                            hoister.GroupInfoID = jituanID;
                            var code = "TSJ";

                            var lastItem = DataContext.Db.Queryable<Hoister>().Where(x => x.Code.Contains(code))
                                .OrderByDescending(x => x.Code)
                                .First();

                            if (lastItem.IsEmpty())
                            {
                                code += "001";
                            }
                            else
                            {
                                var num = lastItem.Code.Right(3).ToInt();
                                code += (++num).ToString().PadLeft(3, '0');
                            }

                           

                            hoister.Code = code;
                            hoister.Name = row.GetCell(2).ToString().Trim();
                            hoister.IsDelete = false;
                            hoister.CreateUserID = userID;
                            hoister.UpdateUserID = userID;
                            hoister.CreateUserName = userName;
                            hoister.UpdateUserName = userName;
                            hoister.CreateDate = DateTime.Now;
                            hoister.UpdateDate = DateTime.Now;

                            hoister.HoisterID = DataContext.Db.Insertable(hoister).ExecuteReturnIdentity();
                            hoisters.Add(hoister);
                            usingWirerope.HoisterID = hoister.HoisterID;

                        }
                        else
                        {
                            usingWirerope.HoisterID = hoistersTemp.HoisterID;
                        }
                    }

                    if (!row.GetCell(5).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.InstallAddressName = row.GetCell(5).ToString().Trim();
                        var hinstallAddressesTemp = installAddresses.FirstOrDefault(x => x.AddressInfo == row.GetCell(5).ToString().Trim());

                        if (hinstallAddressesTemp == null)
                        {
                            InstallAddress installAddress = new InstallAddress();
                            installAddress.GroupInfoID = jituanID;
                            var code = "DD";

                            var lastItem = DataContext.Db.Queryable<InstallAddress>().Where(x => x.Code.Contains(code))
                                .OrderByDescending(x => x.Code)
                                .First();

                            if (lastItem.IsEmpty())
                            {
                                code += "001";
                            }
                            else
                            {
                                var num = lastItem.Code.Right(3).ToInt();
                                code += (++num).ToString().PadLeft(3, '0');
                            }

                           
                            installAddress.Code = code;
                            installAddress.AddressInfo = row.GetCell(5).ToString().Trim();
                            installAddress.IsDelete = false;
                            installAddress.CreateUserID = userID;
                            installAddress.UpdateUserID = userID;
                            installAddress.CreateUserName = userName;
                            installAddress.UpdateUserName = userName;
                            installAddress.CreateDate = DateTime.Now;
                            installAddress.UpdateDate = DateTime.Now;

                            installAddress.InstallAddressID = DataContext.Db.Insertable(installAddress).ExecuteReturnIdentity();
                            installAddresses.Add(installAddress);
                            usingWirerope.InstallAddressID = installAddress.InstallAddressID;
                        }
                        else
                        {
                            usingWirerope.InstallAddressID = hinstallAddressesTemp.InstallAddressID;
                        }
                    }

                    if (!row.GetCell(16).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.MpaName = row.GetCell(16).ToString().Trim();
                        var mpasTemp = mpas.FirstOrDefault(x => x.Name == row.GetCell(16).ToString().Trim());

                        if (mpasTemp == null)
                        {
                            Mpa mpa = new Mpa();
                            mpa.GroupInfoID = jituanID;
                            mpa.Name = row.GetCell(16).ToString().Trim();
                            mpa.IsDelete = false;
                            mpa.CreateUserID = userID;
                            mpa.UpdateUserID = userID;
                            mpa.CreateUserName = userName;
                            mpa.UpdateUserName = userName;
                            mpa.CreateDate = DateTime.Now;
                            mpa.UpdateDate = DateTime.Now;

                            mpa.MpaID = DataContext.Db.Insertable(mpa).ExecuteReturnIdentity();
                            mpas.Add(mpa);
                            usingWirerope.MpaID = mpa.MpaID;
                        }
                        else
                        {
                            usingWirerope.MpaID = mpasTemp.MpaID;
                        }
                    }

                    if (!row.GetCell(17).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.SurfaceStatusName = row.GetCell(17).ToString().Trim();
                        var surfaceTemp = surfaces.FirstOrDefault(x => x.Name == row.GetCell(17).ToString().Trim());

                        if (surfaceTemp == null)
                        {
                            SurfaceStatus surface = new SurfaceStatus();
                            surface.GroupInfoID = jituanID;
                            surface.Name = row.GetCell(17).ToString().Trim();
                            surface.IsDelete = false;
                            surface.CreateUserID = userID;
                            surface.UpdateUserID = userID;
                            surface.CreateUserName = userName;
                            surface.UpdateUserName = userName;
                            surface.CreateDate = DateTime.Now;
                            surface.UpdateDate = DateTime.Now;

                            surface.SurfaceStatusID = DataContext.Db.Insertable(surface).ExecuteReturnIdentity();
                            surfaces.Add(surface);
                            usingWirerope.SurfaceStatusID = surface.SurfaceStatusID;
                        }
                        else
                        {
                            usingWirerope.SurfaceStatusID = surfaceTemp.SurfaceStatusID;
                        }
                    }

                    if (!row.GetCell(18).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.TwistingTypeName = row.GetCell(18).ToString().Trim();

                        var twistingTypesTemp = twistingTypes.FirstOrDefault(x => x.Name == row.GetCell(18).ToString().Trim());

                        if (twistingTypesTemp == null)
                        {
                            TwistingType twistingType = new TwistingType();
                            twistingType.GroupInfoID = jituanID;
                            twistingType.Name = row.GetCell(18).ToString().Trim();
                            twistingType.IsDelete = false;
                            twistingType.CreateUserID = userID;
                            twistingType.UpdateUserID = userID;
                            twistingType.CreateUserName = userName;
                            twistingType.UpdateUserName = userName;
                            twistingType.CreateDate = DateTime.Now;
                            twistingType.UpdateDate = DateTime.Now;

                            twistingType.TwistingTypeID = DataContext.Db.Insertable(twistingType).ExecuteReturnIdentity();
                            twistingTypes.Add(twistingType);
                            usingWirerope.TwistingTypeID = twistingType.TwistingTypeID;
                        }
                        else
                        {
                            usingWirerope.TwistingTypeID = twistingTypesTemp.TwistingTypeID;
                        }
                    }

                    if (!row.GetCell(23).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.SupplierName = row.GetCell(23).ToString().Trim();
                        var suppliersTemp = suppliers.FirstOrDefault(x => x.Name == row.GetCell(23).ToString().Trim());

                        if (suppliersTemp == null)
                        {
                            Supplier supplier = new Supplier();
                            supplier.GroupInfoID = jituanID;
                            var code = "GY";

                            var lastItem = DataContext.Db.Queryable<Supplier>().Where(x => x.Code.Contains(code))
                                .OrderByDescending(x => x.Code)
                                .First();

                            if (lastItem.IsEmpty())
                            {
                                code += "001";
                            }
                            else
                            {
                                var num = lastItem.Code.Right(3).ToInt();
                                code += (++num).ToString().PadLeft(3, '0');
                            }

                            supplier.Code = code;
                            supplier.Name = row.GetCell(23).ToString().Trim();
                            supplier.IsDelete = false;
                            supplier.CreateUserID = userID;
                            supplier.UpdateUserID = userID;
                            supplier.CreateUserName = userName;
                            supplier.UpdateUserName = userName;
                            supplier.CreateDate = DateTime.Now;
                            supplier.UpdateDate = DateTime.Now;

                            supplier.SupplierID = DataContext.Db.Insertable(supplier).ExecuteReturnIdentity();
                            suppliers.Add(supplier);
                            usingWirerope.SupplierID = supplier.SupplierID;
                        }
                        else
                        {
                            usingWirerope.SupplierID = suppliersTemp.SupplierID;
                        }
                    }

                    if (!row.GetCell(24).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.ProducerName = row.GetCell(24).ToString().Trim();
                        var producersTemp = producers.FirstOrDefault(x => x.Name == row.GetCell(24).ToString().Trim());

                        if (producersTemp == null)
                        {
                            Producer producer = new Producer();
                            producer.GroupInfoID = jituanID;
                            var code = "ZZ";

                            var lastItem = DataContext.Db.Queryable<Producer>().Where(x => x.Code.Contains(code))
                                .OrderByDescending(x => x.Code)
                                .First();

                            if (lastItem.IsEmpty())
                            {
                                code += "001";
                            }
                            else
                            {
                                var num = lastItem.Code.Right(3).ToInt();
                                code += (++num).ToString().PadLeft(3, '0');
                            }

                          
                            producer.Code = code;
                            producer.Name = row.GetCell(24).ToString().Trim();
                            producer.IsDelete = false;
                            producer.CreateUserID = userID;
                            producer.UpdateUserID = userID;
                            producer.CreateUserName = userName;
                            producer.UpdateUserName = userName;
                            producer.CreateDate = DateTime.Now;
                            producer.UpdateDate = DateTime.Now;

                            producer.ProducerID = DataContext.Db.Insertable(producer).ExecuteReturnIdentity();
                            producers.Add(producer);
                            usingWirerope.ProducerID = producer.ProducerID;
                        }
                        else
                        {
                            usingWirerope.ProducerID = producersTemp.ProducerID;
                        }
                    }

                    if (!row.GetCell(20).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.HoisterUsageName = row.GetCell(20).ToString().Trim();
                        var hoisterUsagesTemp = hoisterUsages.FirstOrDefault(x => x.Name == row.GetCell(20).ToString().Trim());

                        if (hoisterUsagesTemp == null)
                        {
                            HoisterUsage hoisterUsage = new HoisterUsage();
                            hoisterUsage.GroupInfoID = jituanID;
                            hoisterUsage.Name = row.GetCell(20).ToString().Trim();
                            hoisterUsage.HoisterID = usingWirerope.HoisterID;
                            hoisterUsage.IsDelete = false;
                            hoisterUsage.CreateUserID = userID;
                            hoisterUsage.UpdateUserID = userID;
                            hoisterUsage.CreateUserName = userName;
                            hoisterUsage.UpdateUserName = userName;
                            hoisterUsage.CreateDate = DateTime.Now;
                            hoisterUsage.UpdateDate = DateTime.Now;

                            hoisterUsage.HoisterUsageID = DataContext.Db.Insertable(hoisterUsage).ExecuteReturnIdentity();
                            hoisterUsages.Add(hoisterUsage);
                            usingWirerope.HoisterUsageID = hoisterUsage.HoisterUsageID;
                        }
                        else
                        {
                            usingWirerope.HoisterUsageID = hoisterUsagesTemp.HoisterUsageID;
                        }
                    }

                    if (!row.GetCell(21).ToString().Trim().IsEmpty())
                    {
                        usingWirerope.WireropeTypeName = row.GetCell(21).ToString().Trim();
                        var wireropeTypesTemp = wireropeTypes.FirstOrDefault(x => x.Name == row.GetCell(21).ToString().Trim());

                        if (wireropeTypesTemp == null)
                        {
                            WireropeType wireropeType = new WireropeType();
                            wireropeType.GroupInfoID = jituanID;
                            wireropeType.HoisterID = usingWirerope.HoisterID;
                            wireropeType.Name = row.GetCell(21).ToString().Trim();
                            wireropeType.IsDelete = false;
                            wireropeType.CreateUserID = userID;
                            wireropeType.UpdateUserID = userID;
                            wireropeType.CreateUserName = userName;
                            wireropeType.UpdateUserName = userName;
                            wireropeType.CreateDate = DateTime.Now;
                            wireropeType.UpdateDate = DateTime.Now;

                            wireropeType.WireropeTypeID = DataContext.Db.Insertable(wireropeType).ExecuteReturnIdentity();
                            wireropeTypes.Add(wireropeType);
                            usingWirerope.WireropeTypeID = wireropeType.WireropeTypeID;
                        }
                        else
                        {
                            usingWirerope.WireropeTypeID = wireropeTypesTemp.WireropeTypeID;
                        }
                    }

                    usingWirerope.UsingWireropeID = DataContext.Db.Insertable(usingWirerope).ExecuteReturnIdentity();

                    usingWireropes.Add(usingWirerope);
                }


                //获得在用绳检测excel数据
                var sheetCheckWirerope = workbook.GetSheetAt(1);
                for (int i = 1; i <= sheetCheckWirerope.LastRowNum; i++)
                {
                    var row = sheetCheckWirerope.GetRow(i);

                    CheckWirerope checkWirerope = new CheckWirerope();

                    //通过绳子编号获得在用绳数据
                    var usingWirrropeInfo = usingWireropes.FirstOrDefault(x => x.Code == row.GetCell(1).ToString().Trim());
                    if (usingWirrropeInfo != null)
                    {
                        checkWirerope.GroupInfoID = jituanID;
                        checkWirerope.UsingWireropeID = usingWirrropeInfo.UsingWireropeID;
                        checkWirerope.CheckUnit = row.GetCell(2).ToString().Trim();
                        checkWirerope.CheckUserName = row.GetCell(3).ToString().Trim();
                        checkWirerope.CheckDate = row.GetCell(4).ToString().Trim() == "" ? DateTime.Now : row.GetCell(4).ToString().Trim().ToDateTime();
                        checkWirerope.NextCheckDate = row.GetCell(5).ToString().Trim() == "" ? DateTime.Now : row.GetCell(5).ToString().Trim().ToDateTime();
                        checkWirerope.Status = 200;
                        checkWirerope.CreateUserID = userID;
                        checkWirerope.CreateUserName = userName;
                        checkWirerope.CreateDate = DateTime.Now;
                        checkWirerope.SubmitUserID = userID;
                        checkWirerope.SubmitUserName = userName;
                        checkWirerope.SubmitDate = checkWirerope.CheckDate;
                        checkWirerope.CheckCode = row.GetCell(6).ToString().Trim();

                        checkWirerope.CheckWireropeID = DataContext.Db.Insertable(checkWirerope).ExecuteReturnIdentity();

                        checkWireropes.Add(checkWirerope);
                    }
                    else
                    {
                        Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim(), "SQL导入日志-在用绳检测绳子错误-");
                    }
                }


                //获得在用绳检测详情excel数据
                var sheetChecWireropeDetail = workbook.GetSheetAt(2);
                for (int i = 1; i <= sheetChecWireropeDetail.LastRowNum; i++)
                {
                    var row = sheetChecWireropeDetail.GetRow(i);

                    CheckWireropeDetail checkWireropeDetail = new CheckWireropeDetail();

                    //通过绳子编号获得在用绳数据
                    var usingWirrropeInfo = usingWireropes.FirstOrDefault(x => x.Code == row.GetCell(1).ToString().Trim());
                    if (usingWirrropeInfo != null)
                    {
                        //通过绳子编号获得在用绳检测详情
                        var checkWirrropeInfo = checkWireropes.FirstOrDefault(x => x.CheckCode == row.GetCell(14).ToString().Trim());
                        if (checkWirrropeInfo != null)
                        {
                            checkWireropeDetail.GroupInfoID = jituanID;
                            checkWireropeDetail.UsingWireropeID = usingWirrropeInfo.UsingWireropeID;
                            checkWireropeDetail.CheckWireropeID = checkWirrropeInfo.CheckWireropeID;
                            checkWireropeDetail.SortNo = row.GetCell(2).ToString().Trim() == "" ? default(int) : row.GetCell(2).ToString().Trim().ToInt();
                            checkWireropeDetail.Name = row.GetCell(3).ToString().Trim();
                            checkWireropeDetail.DsCnt = row.GetCell(4).ToString().Trim() == "" ? default(int) : row.GetCell(4).ToString().Trim().ToInt();
                            checkWireropeDetail.DsLa = row.GetCell(5).ToString().Trim() == "" ? default(decimal) : row.GetCell(5).ToString().Trim().ToDecimal();
                            checkWireropeDetail.ZjPosition = row.GetCell(6).ToString().Trim() == "" ? default(decimal) : row.GetCell(6).ToString().Trim().ToDecimal();
                            checkWireropeDetail.ZjDia = row.GetCell(7).ToString().Trim() == "" ? default(decimal) : row.GetCell(7).ToString().Trim().ToDecimal();
                            checkWireropeDetail.JsjmPosition = row.GetCell(8).ToString().Trim() == "" ? default(decimal) : row.GetCell(8).ToString().Trim().ToDecimal();
                            checkWireropeDetail.JsjmLma = row.GetCell(9).ToString().Trim() == "" ? default(decimal) : row.GetCell(9).ToString().Trim().ToDecimal();
                            checkWireropeDetail.OthFuShi = row.GetCell(10).ToString().Trim();
                            checkWireropeDetail.OthMoSun = row.GetCell(11).ToString().Trim();
                            checkWireropeDetail.OthQiTa = row.GetCell(12).ToString().Trim();
                            checkWireropeDetail.OthZongHe = row.GetCell(13).ToString().Trim();

                            checkWireropeDetail.CheckWireropeDetailID = DataContext.Db.Insertable(checkWireropeDetail).ExecuteReturnIdentity();

                            checkWireropeDetails.Add(checkWireropeDetail);
                        }
                        else
                        {
                            Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim() + "  ,usingWirrropeInfoID: " + usingWirrropeInfo.UsingWireropeID, "SQL导入日志-在用绳检测详情错误-检测-");
                        }
                    }
                    else
                    {
                        Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim(), "SQL导入日志-在用绳检测详情错误-");
                    }
                }


                //获得在用绳检测数据excel数据
                var sheetDetailData = workbook.GetSheetAt(3);
                for (int i = 1; i <= sheetDetailData.LastRowNum; i++)
                {
                    var row = sheetDetailData.GetRow(i);

                    CheckWireropeDetailData checkWireropeDetailData = new CheckWireropeDetailData();

                    //通过绳子编号获得在用绳数据
                    var usingWirrropeInfo = usingWireropes.FirstOrDefault(x => x.Code == row.GetCell(1).ToString().Trim());
                    if (usingWirrropeInfo != null)
                    {
                        //通过绳子编号获得在用绳检测详情
                        var checkWirrropeInfo = checkWireropes.FirstOrDefault(x => x.CheckCode == row.GetCell(7).ToString().Trim());
                        if (checkWirrropeInfo != null)
                        {
                            var checkDetailInfo = checkWireropeDetails.FirstOrDefault(x => x.CheckWireropeID == checkWirrropeInfo.CheckWireropeID && x.SortNo == row.GetCell(2).ToString().Trim().ToInt() && x.Name == row.GetCell(3).ToString().Trim());

                            if (checkDetailInfo != null)
                            {
                                checkWireropeDetailData.GroupInfoID = jituanID;
                                checkWireropeDetailData.UsingWireropeID = usingWirrropeInfo.UsingWireropeID;
                                checkWireropeDetailData.CheckWireropeID = checkWirrropeInfo.CheckWireropeID;
                                checkWireropeDetailData.CheckWireropeDetailID = checkDetailInfo.CheckWireropeDetailID;
                                checkWireropeDetailData.Type = row.GetCell(4).ToString().Trim() == "断丝" ? 1 : 2;
                                checkWireropeDetailData.Position = row.GetCell(5).ToString().Trim() == "" ? default(decimal) : row.GetCell(5).ToString().Trim().ToDecimal();
                                checkWireropeDetailData.Num = row.GetCell(6).ToString().Trim() == "" ? default(decimal) : row.GetCell(6).ToString().Trim().ToDecimal();


                                DataContext.Db.Insertable(checkWireropeDetailData).ExecuteCommand();
                            }
                            else
                            {
                                Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim() + "  ,usingWirrropeInfoID: " + usingWirrropeInfo.UsingWireropeID + "  ,CheckWireropeID: " + checkWirrropeInfo.CheckWireropeID, "SQL导入日志-在用绳检测数据错误-检测-详情-");
                            }
                        }
                        else
                        {
                            Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim() + "  ,usingWirrropeInfoID: " + usingWirrropeInfo.UsingWireropeID, "SQL导入日志-在用绳检测数据错误-检测-");
                        }
                    }
                    else
                    {
                        Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim(), "SQL导入日志-在用绳检测数据错误-");
                    }


                }


                //获得在用绳检测位置excel数据
                var sheetDetailPosition = workbook.GetSheetAt(4);
                for (int i = 1; i <= sheetDetailPosition.LastRowNum; i++)
                {
                    var row = sheetDetailPosition.GetRow(i);
                    CheckWireropeDetailPosition checkWireropeDetailPosition = new CheckWireropeDetailPosition();

                    //通过绳子编号获得在用绳数据
                    var usingWirrropeInfo = usingWireropes.FirstOrDefault(x => x.Code == row.GetCell(1).ToString().Trim());
                    if (usingWirrropeInfo != null)
                    {
                        //通过绳子编号获得在用绳检测详情
                        var checkWirrropeInfo = checkWireropes.FirstOrDefault(x => x.CheckCode == row.GetCell(6).ToString().Trim());
                        if (checkWirrropeInfo != null)
                        {
                            var checkDetailInfo = checkWireropeDetails.FirstOrDefault(x => x.CheckWireropeID == checkWirrropeInfo.CheckWireropeID && x.SortNo == row.GetCell(2).ToString().Trim().ToInt() && x.Name == row.GetCell(3).ToString().Trim());

                            if (checkDetailInfo != null)
                            {
                                checkWireropeDetailPosition.GroupInfoID = jituanID;
                                checkWireropeDetailPosition.UsingWireropeID = usingWirrropeInfo.UsingWireropeID;
                                checkWireropeDetailPosition.CheckWireropeID = checkWirrropeInfo.CheckWireropeID;
                                checkWireropeDetailPosition.CheckWireropeDetailID = checkDetailInfo.CheckWireropeDetailID;
                                checkWireropeDetailPosition.DsStart = row.GetCell(4).ToString().Trim() == "" ? default(decimal) : row.GetCell(4).ToString().Trim().ToDecimal();
                                checkWireropeDetailPosition.DsEnd = row.GetCell(5).ToString().Trim() == "" ? default(decimal) : row.GetCell(5).ToString().Trim().ToDecimal();


                                DataContext.Db.Insertable(checkWireropeDetailPosition).ExecuteCommand();
                            }
                            else
                            {
                                Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim() + "  ,usingWirrropeInfoID: " + usingWirrropeInfo.UsingWireropeID + "  ,CheckWireropeID: " + checkWirrropeInfo.CheckWireropeID, "SQL导入日志-在用绳检测位置错误-检测-详情-");
                            }
                        }
                        else
                        {
                            Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim() + "  ,usingWirrropeInfoID: " + usingWirrropeInfo.UsingWireropeID, "SQL导入日志-在用绳检测位置错误-检测-");
                        }
                    }
                    else
                    {
                        Gx.Log("找不到绳子，编码：" + row.GetCell(1).ToString().Trim(), "SQL导入日志-在用绳检测位置错误-");
                    }
                }
            }

            //处理检测状态数据
            //1.检索所有在用绳数据
            foreach (var item in usingWireropes)
            {
                CheckWireropeStatusMonitor checkWireropeStatusMonitor = new CheckWireropeStatusMonitor();
                checkWireropeStatusMonitor.GroupInfoID = jituanID;
                checkWireropeStatusMonitor.UsingWireropeID = item.UsingWireropeID;


                //2.获得该在用绳的检测数据（最新的一条数据）
                var checkTemp = checkWireropes.Where(x => x.UsingWireropeID == item.UsingWireropeID && x.SubmitDate.HasValue).OrderByDescending(x => x.SubmitDate.Value).FirstOrDefault();
                if (checkTemp != null)
                {
                    //3.获得最新一条的检测数据中的数据详情，获得LA，DIA,LMA分别的最大值
                    var detailList = checkWireropeDetails.Where(x => x.CheckWireropeID == checkTemp.CheckWireropeID).ToList();

                    var LAMax = detailList.Max(x => x.DsLa).Value;

                    var DIAMax = detailList.Max(x => x.ZjDia).Value;

                    var LMAMax = detailList.Max(x => x.JsjmLma).Value;


                    //4.根据基础数据(通过提升机类型，提升用途，钢丝绳类型)，获得LA，DIA,LMA3种分别是什么状态
                    var LAStandStatus = standStatus.FirstOrDefault(x => x.GroupInfoID == jituanID && x.Type == (int)StandStatus_Type.LA);
                    var LAStandStatusDetail = new StandStatusDetail();
                    if (LAStandStatus != null)
                    {
                        LAStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == LAStandStatus.StandStatusID && x.HoisterID == item.HoisterID && x.HoisterUsageID == item.HoisterUsageID && x.WireropeTypeID == item.WireropeTypeID).FirstOrDefault();

                        if (LAStandStatusDetail != null)
                        {
                            if (LAMax >= 0 && LAMax < LAStandStatusDetail.GreenLight)
                            {
                                checkWireropeStatusMonitor.La = (int)StatusMonitor_LightType.绿;
                            }
                            else if (LAMax >= LAStandStatusDetail.GreenLight && LAMax <= LAStandStatusDetail.BlueLight)
                            {
                                checkWireropeStatusMonitor.La = (int)StatusMonitor_LightType.蓝;
                            }
                            else if (LAMax >= LAStandStatusDetail.BlueLight && LAMax <= LAStandStatusDetail.YellowLight)
                            {
                                checkWireropeStatusMonitor.La = (int)StatusMonitor_LightType.黄;
                            }
                            else if (LAMax >= LAStandStatusDetail.YellowLight && LAMax <= LAStandStatusDetail.OrangeLight)
                            {
                                checkWireropeStatusMonitor.La = (int)StatusMonitor_LightType.橙;
                            }
                            else
                            {
                                checkWireropeStatusMonitor.La = (int)StatusMonitor_LightType.灰;
                            }
                        }

                    }

                    var DIAStandStatus = standStatus.FirstOrDefault(x => x.GroupInfoID == jituanID && x.Type == (int)StandStatus_Type.DIA);
                    var DIAStandStatusDetail = new StandStatusDetail();
                    if (DIAStandStatus != null)
                    {
                        DIAStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == DIAStandStatus.StandStatusID && x.HoisterID == item.HoisterID && x.HoisterUsageID == item.HoisterUsageID && x.WireropeTypeID == item.WireropeTypeID).FirstOrDefault();

                        if (DIAStandStatusDetail != null)
                        {
                            if (DIAMax >= 0 && DIAMax < DIAStandStatusDetail.GreenLight)
                            {
                                checkWireropeStatusMonitor.Dia = (int)StatusMonitor_LightType.绿;
                            }
                            else if (DIAMax >= DIAStandStatusDetail.GreenLight && DIAMax <= DIAStandStatusDetail.BlueLight)
                            {
                                checkWireropeStatusMonitor.Dia = (int)StatusMonitor_LightType.蓝;
                            }
                            else if (DIAMax >= DIAStandStatusDetail.BlueLight && DIAMax <= DIAStandStatusDetail.YellowLight)
                            {
                                checkWireropeStatusMonitor.Dia = (int)StatusMonitor_LightType.黄;
                            }
                            else if (DIAMax >= DIAStandStatusDetail.YellowLight && DIAMax <= DIAStandStatusDetail.OrangeLight)
                            {
                                checkWireropeStatusMonitor.Dia = (int)StatusMonitor_LightType.橙;
                            }
                            else
                            {
                                checkWireropeStatusMonitor.Dia = (int)StatusMonitor_LightType.灰;
                            }
                        }

                    }

                    var LMAStandStatus = standStatus.FirstOrDefault(x => x.GroupInfoID == jituanID && x.Type == (int)StandStatus_Type.LMA);

                    if (LMAStandStatus != null)
                    {
                        var LMAStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == LMAStandStatus.StandStatusID && x.HoisterID == item.HoisterID && x.HoisterUsageID == item.HoisterUsageID && x.WireropeTypeID == item.WireropeTypeID).FirstOrDefault();

                        if (LMAStandStatusDetail != null)
                        {
                            if (LMAMax >= 0 && LMAMax < LMAStandStatusDetail.GreenLight)
                            {
                                checkWireropeStatusMonitor.Lma = (int)StatusMonitor_LightType.绿;
                            }
                            else if (LMAMax >= LMAStandStatusDetail.GreenLight && LMAMax <= LMAStandStatusDetail.BlueLight)
                            {
                                checkWireropeStatusMonitor.Lma = (int)StatusMonitor_LightType.蓝;
                            }
                            else if (LMAMax >= LMAStandStatusDetail.BlueLight && LMAMax <= LMAStandStatusDetail.YellowLight)
                            {
                                checkWireropeStatusMonitor.Lma = (int)StatusMonitor_LightType.黄;
                            }
                            else if (LMAMax >= LMAStandStatusDetail.YellowLight && LMAMax <= LMAStandStatusDetail.OrangeLight)
                            {
                                checkWireropeStatusMonitor.Lma = (int)StatusMonitor_LightType.橙;
                            }
                            else
                            {
                                checkWireropeStatusMonitor.Lma = (int)StatusMonitor_LightType.灰;
                            }
                        }
                    }



                    //5.获得当前基础数据中的LA的橙灯的数据(LAp)，获得DIA的橙灯数据(DIAp)
                    if (LAStandStatusDetail != null && DIAStandStatusDetail != null)
                    {
                        if (LAStandStatusDetail.OrangeLight.HasValue && DIAStandStatusDetail.OrangeLight.HasValue)
                        {
                            var LAp = LAStandStatusDetail.OrangeLight.Value;
                            var DIAp = DIAStandStatusDetail.OrangeLight.Value;

                            //6.通过综合状态的计算公式，DF=LA/LAp +DIA/DIAp ，计算综合状态的值
                            var DF = Math.Round(LAMax / LAp + DIAMax / DIAp, 2);

                            //7.根据基础数据(通过提升机类型，提升用途，钢丝绳类型)，获得综合是什么状态
                            var DFStandStatus = standStatus.FirstOrDefault(x => x.GroupInfoID == jituanID && x.Type == (int)StandStatus_Type.综合);

                            if (DFStandStatus != null)
                            {
                                var DFStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == DFStandStatus.StandStatusID && x.HoisterID == item.HoisterID && x.HoisterUsageID == item.HoisterUsageID && x.WireropeTypeID == item.WireropeTypeID).FirstOrDefault();

                                if (DFStandStatusDetail != null)
                                {
                                    if (DF >= 0 && DF < DFStandStatusDetail.GreenLight)
                                    {
                                        checkWireropeStatusMonitor.Summary = (int)StatusMonitor_LightType.绿;
                                    }
                                    else if (DF >= DFStandStatusDetail.GreenLight && DF <= DFStandStatusDetail.BlueLight)
                                    {
                                        checkWireropeStatusMonitor.Summary = (int)StatusMonitor_LightType.蓝;
                                    }
                                    else if (DF >= DFStandStatusDetail.BlueLight && DF <= DFStandStatusDetail.YellowLight)
                                    {
                                        checkWireropeStatusMonitor.Summary = (int)StatusMonitor_LightType.黄;
                                    }
                                    else if (DF >= DFStandStatusDetail.YellowLight && DF <= DFStandStatusDetail.OrangeLight)
                                    {
                                        checkWireropeStatusMonitor.Summary = (int)StatusMonitor_LightType.橙;
                                    }
                                    else
                                    {
                                        checkWireropeStatusMonitor.Summary = (int)StatusMonitor_LightType.灰;
                                    }
                                }
                            }
                        }
                    }

                    //8.插入数据
                    DataContext.Db.Insertable(checkWireropeStatusMonitor).ExecuteReturnIdentity();

                }
            }

            MessageBox.Show("OK");
        }
    }
}